package cn.maidouya.dictionary.controller;
import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.dictionary.domain.DictionaryType;
import cn.maidouya.dictionary.query.DictionaryTypeQuery;
import cn.maidouya.dictionary.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
@Api(tags = "字典类型接口",description = "字典类型接口")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    @PutMapping
    @ApiOperation(value = "添加和修改字典类型",notes = "添加和修改字典类型")
    public AjaxResult addAndUpdata(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId() == null) {
                dictionaryTypeService.add(dictionaryType);
            }else {
                dictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("操作失败:" +e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除字典类型",notes = "删除字典类型")
    public AjaxResult del(@PathVariable Long id){
        System.out.println("DictionaryType接收到的： " + id);
        try {
            dictionaryTypeService.del(id);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("操作失败:" +e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查找一个字典类型",notes = "查找一个字典类型")
    public DictionaryType findOne(@PathVariable("id") Long id){
   return dictionaryTypeService.finOne(id);
    }
    @GetMapping
    @ApiOperation(value = "查找全部典类型",notes = "查找全部典类型")
    public List<DictionaryType> findAll(){
        return dictionaryTypeService.finAll();
    }
    @PostMapping
    @ApiOperation(value = "查找分页典类型",notes = "查找分页典类型")
    public PageList<DictionaryType> findPage(@RequestBody DictionaryTypeQuery dictionaryTypeQuery){
        return dictionaryTypeService.findPage(dictionaryTypeQuery);
    }
}